package com.clhbolg.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.clhbolg.domain.OperationLog;
import com.clhbolg.domain.bo.OperationLogBo;
import com.clhbolg.domain.vo.OperationLogVo;
import com.clhbolg.mapper.OperationLogMapper;
import com.clhbolg.service.IOperationLogService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author cailinhu
 * @date 2023-02-08
 */
@RequiredArgsConstructor
@Service
@DS("slave")
public class OperationLogServiceImpl implements IOperationLogService {

    private final OperationLogMapper baseMapper;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public OperationLogVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<OperationLogVo> queryPageList(OperationLogBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OperationLog> lqw = buildQueryWrapper(bo);
        Page<OperationLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<OperationLogVo> queryList(OperationLogBo bo) {
        LambdaQueryWrapper<OperationLog> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OperationLog> buildQueryWrapper(OperationLogBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OperationLog> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getOptModule()), OperationLog::getOptModule, bo.getOptModule());
        lqw.eq(StringUtils.isNotBlank(bo.getOptType()), OperationLog::getOptType, bo.getOptType());
        lqw.eq(StringUtils.isNotBlank(bo.getOptUri()), OperationLog::getOptUri, bo.getOptUri());
        lqw.eq(StringUtils.isNotBlank(bo.getOptMethod()), OperationLog::getOptMethod, bo.getOptMethod());
        lqw.eq(StringUtils.isNotBlank(bo.getOptDesc()), OperationLog::getOptDesc, bo.getOptDesc());
        lqw.eq(StringUtils.isNotBlank(bo.getRequestParam()), OperationLog::getRequestParam, bo.getRequestParam());
        lqw.eq(StringUtils.isNotBlank(bo.getRequestMethod()), OperationLog::getRequestMethod, bo.getRequestMethod());
        lqw.eq(StringUtils.isNotBlank(bo.getResponseData()), OperationLog::getResponseData, bo.getResponseData());
        lqw.eq(bo.getUserId() != null, OperationLog::getUserId, bo.getUserId());
        lqw.like(StringUtils.isNotBlank(bo.getNickname()), OperationLog::getNickname, bo.getNickname());
        lqw.eq(StringUtils.isNotBlank(bo.getIpAddress()), OperationLog::getIpAddress, bo.getIpAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getIpSource()), OperationLog::getIpSource, bo.getIpSource());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(OperationLogBo bo) {
        OperationLog add = BeanUtil.toBean(bo, OperationLog.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(OperationLogBo bo) {
        OperationLog update = BeanUtil.toBean(bo, OperationLog.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(OperationLog entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
